/**
 * Created by joustany on 17/11/23.
 */
var sphone = window.localStorage.phone
console.log(window.localStorage.url)
$(function () {
    getShowView()
})
function getShowView() {
    "use strict"
    var params = JSON.parse(window.localStorage.user);
    console.log(params)
    var redirect = JSON.parse(decodeURIComponent(params['redirect']));
    console.log(redirect)
    $.ajax({
        url: configApi('/coupon/share-other'),
        data: {
            share_id:redirect.share_id,
            access_token:redirect.token
        },
        dataType: 'json',
        type: "get",
    }).done(function (res) {
        console.log(res)
        luckView(res.data)
    }).fail(function () {
        layer.open({
            content: "服务器异常,请退出重试"
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    })
}
function luckView(data) {
    "use strict"
    //二次分享
    // console.log($('.share—alipay .title').attr('content',))
    var shareText = data.text
    var wx_title = shareText.title,
        wx_desc = shareText.desc,
        wx_img = shareText.icon,
        shareId = window.localStorage.url,
        wx_link = window.location.origin + '/webApp/shareParams.html?lineLink='+ shareId +''
    if(params.platform == "wechat"){
        wxShare(wx_title,wx_desc,wx_img,wx_link)
    }else {
        $('.share—alipay .title').attr('content',wx_title)
        $('.share—alipay .icon').attr('content',wx_img)
        $('.share—alipay .desc').attr('content',wx_desc)
        $('.share—alipay .link').attr('content',wx_link)
    }
    //判断是否注册
    if (sphone){
        share()
        $('.vouchers').show()
        $('.login').hide()
    }else {
        $('.login').show()
        $('.vouchers').hide()
        $('.content').css('padding-bottom','0')
    }
    var luck = data.coupon,
        html = ''
    if ($.isEmptyObject(luck)){
        $('.luck-container').hide()
    }else {
        $.each(luck,function (index,item) {
            console.log(item)
            $('.luck-container').show()
            html +='<div class="luck-worker">'
            html +='<div class="luck-user">'
            html +='<img src="'+ item.user.avatar +'" alt="">'
            html +='<span class="name-con"><span class="name">'+ item.user.username+'</span><span class="date">'+ item.created_at+'</span></span>'
            html +='</div>'
            html +='<div class="price"><span class="num">'+item.money+'</span>元</div>'
            html +='</div>'
        })
    }
    $('.luck-content').html(html)
}
/**
 * 获取验证码
 * */
var count = 60; //倒计时时间
var InterValObj //控制倒计时
var params = JSON.parse(window.localStorage.user); //参数
var redirect = JSON.parse(decodeURIComponent(params['redirect']))//参数
$(document).on('click','.show_code',function () {
    "use strict"
    var access_token = redirect.token
    var phone = $(".phone input")[0].value
    console.log(phone)
    var phoneVerifg = /^1[34578]\d{9}$/;
    console.log(redirect.phone)
    if(!phoneVerifg.test(phone)){
        layer.open({
            content: '您输入的手机号有误'
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    }else {
        $('.show_code').addClass('disabled')
        $('.show_code').removeClass('show_code')
        $(".show_code").html(count + "秒后重试");
        $.ajax({
            url: configApi('/sms/send'),
            data: {
                phone:phone,
                type: 'region',
                access_token: access_token
            },
            dataType: 'json',
            type: "POST",
        }).done(function (res) {
            // alert(res.message)
            if (res.success){
                $('.obtain').addClass('disabled')
                $(".obtain").html(count + "秒后重试");
                InterValObj = window.setInterval(SetRemainTime, 1000)
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }else {
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }).fail(function () {
            layer.open({
                content: "服务器异常,请退出重试"
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        })
    }
})
//倒计时
function SetRemainTime() {
    console.log(count)
    if (count == 0) {
        window.clearInterval(InterValObj);//停止计时器
        $('.disabled').addClass('show_code')
        $(".show_code").removeClass("disabled");//启用按钮
        $(".show_code").html("获取验证码");
        count = 60
    }
    else {
        count--;
        $(".disabled").html(count + "秒后重试");
    }
}
/**
 * 立即领取
 * */
$(document).on('click','.submit',function () {
    var phone = $(".phone input")[0].value
    var code = $(".code input")[0].value
    if(code.length == 4){
        //领取注册红包
        $.ajax({
            url: configApi('/user/region'),
            data: {
                share_id:redirect.share_id,
                phone:phone,
                code: code,
                access_token: redirect.token
            },
            dataType: 'json',
            type: "POST",
        }).done(function (res) {
            "use strict"
                var data = res.data
                //判断是否领过注册红包
            if (res.success){
                if (res.code == '1001'){
                    $('.rests—container').hide()
                    share()
                }else {
                    $('.rests—container').show()
                    loginSucceed(data)
                    share()
                }
                console.log(params)
                window.localStorage.phone = phone
            }else {
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }).fail(function () {
            layer.open({
                content: "服务器异常,请退出重试"
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        })
    }else {
        layer.open({
            content: '您输入的验证码有误'
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    }
})
//领取分享红包
function share() {
    $.ajax({
        url: configApi('/coupon/share'),
        data: {
            share_id:redirect.share_id,
            access_token: redirect.token
        },
        dataType: 'json',
        type: "POST",
    }).done(function (res) {
        console.log(res)
        var code = res.code
        if (res.success){
            var data = res.data
            //判断是否领过
            if (code == '1011'){
            $.each(data,function (index,item) {
                "use strict"
                // console.log(item)
                if (item.type == '2'){
                    getSucceed(item)
                }
                if(item.type == '3'){
                    loginSucceed(item)
                    $('.rests—container').show()
                }
            })
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $('.login-container').fadeOut('fast');
                $('.vouchers').fadeIn();
                window.clearInterval(InterValObj)
            }else {
                getSucceed(data)
                $('.login-container').fadeOut('fast');
                $('.vouchers').fadeIn();

            }
        }else {
            window.clearInterval(InterValObj)

            if(code == '1012'){
                $('.content').hide()
                $('.no-container').show()
            }else if(code == '1013'){
                $('.no-container .no-title').text('来晚了, 红包抢完拉～')
                $('.no-container .no-message').text('手气和运气同样重要')
                $('.no-container .no-img').attr('src','./images/ui_icon/红包.png')
                $('.content').hide()
                $('.no-container').show()
            }else{
                $('.login-container').fadeOut('fast');
                $('.vouchers').fadeIn();

                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }
    }).fail(function () {
        layer.open({
            content: "服务器异常,请退出重试"
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    })
}
function getSucceed(data) {
    "use strict"
    // console.log(data)
        $('.voucher-container .title').text(data.title)
        $('.voucher-container .full').text('满'+data.money_limit+'元可用')
        $('.voucher-container .num').text(data.money)
        $('.voucher-container .indate .date').text(data.etime)
        $('.message').text('红包已发放至'+data.phone)
}
function loginSucceed(data) {
    // console.log(data)
    $('.rests—container .num').text(data.money)
    $('.rests—container .title').text(data.title)
    $('.rests—container .full').text(data.desc)
    $('.rests—container .date').text(data.etime)
}

